import { app } from "electron";
import fs from "fs";
import { join } from "path";
import { cmdExec,PATH } from "../utils";
import { snapShotCmd } from "./cmdConfig";
import { taskNameGenerator } from "./taskNameGenerator";

export const getSnapShotByHdc = async (taskName, SN, OS, testType) => {
  const SNFilePath = SN.replaceAll(":", "_");
  const dataRootPath=PATH.dataPath;
  if(!fs.existsSync(dataRootPath)){
    if(!fs.existsSync(`D:\\ProgramData`)){
      fs.mkdirSync(`D:\\ProgramData`)
    }
    fs.mkdirSync(dataRootPath);
  }
  const localPath = join(dataRootPath, SNFilePath, taskName);
  if (!fs.existsSync(localPath)) {
    taskName = taskNameGenerator(SN, testType);
  }
  console.log("OS: " + OS);
  const osInfo = OS.toLocaleString().replaceAll("-", "_").replaceAll(".", "_");
  const timeNow = new Date().toLocaleTimeString().replaceAll(":", "_");
  const snapShotName = osInfo + "_" + testType + "_" + timeNow;
  console.log("snapShotName: " + snapShotName);

  await cmdExec(snapShotCmd.hdc.mkdir(SN));
  await cmdExec(snapShotCmd.hdc.chmod(SN));

  const fileName = snapShotCmd.hdc.filePath + snapShotName + ".jpeg";

  console.log("fileName: " + fileName);
  let snapShot = await cmdExec(snapShotCmd.hdc.snapshot(SN, fileName));
  console.log(
    "******11*****" + snapShotCmd.hdc.snapshot(SN, fileName) + "\n" + snapShot
  );
  let result = await cmdExec(snapShotCmd.hdc.recv(SN, fileName, localPath));
  console.log("**********12**********" + result);
  return { localPath, snapShotName };
};
